1151F - Sonya and Informatics - CodeForces Solution


combinatorics dp matrices probabilities *2300

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def matmul(a, b, n):
    c = []
    for i in range(n):
        for j in range(n):
            x = 0
            for k in range(n):
                x += a[n * i + k] * b[n * k + j] % mod
                x %= mod
            c.append(x)
    return c

n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = pow(10, 9) + 7
s = n - sum(a)
m = min(s, n - s) + 1
x = [0] * (m * m)
b = list(a)
b.sort()
for i in range(m):
    for j in range(n):
        for l in range(j + 1, n):
            if b[j] == b[l]:
                x[i * m + i] += 1
                continue
            b[j], b[l] = b[l], b[j]
            c = 0
            for u in range(s):
                c += b[u]
            x[i * m + c] += 1
            b[j], b[l] = b[l], b[j]
    if i ^ (m - 1):
        b[i], b[-i - 1] = b[-i - 1], b[i]
y = [min(i % (m + 1), 1) ^ 1 for i in range(m * m)]
z = pow(n * (n - 1) // 2, k, mod)
invz = pow(z, mod - 2, mod)
p = 1
while k:
    if k & p:
        y = matmul(x, y, m)
        k ^= p
    x = matmul(x, x, m)
    p *= 2
ans = y[sum(a[:s]) * m] * invz % mod
print(ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll mod=1e9+7;
ll qpow(ll a,ll b){ll res=1;while(b){if(b&1)res=res*a%mod;a=a*a%mod,b>>=1;}return res;}
const int N=102;
int n,k,m,x,a[N];
int A[N],B[N],C[N];
struct mul{
	int n;
    ll g[N][N];
	mul(int n_){n=n_;memset(g,0,sizeof g);}
    mul operator *(mul &y){
        mul c(y.n);
        for(int i=0;i<n;i++)
        for(int k=0;k<n;k++)
        for(int j=0;j<n;j++)
        c.g[i][j]=(c.g[i][j]+g[i][k]*y.g[k][j])%mod;
        return c;
    }
};
inline mul ksm(mul a,ll b){
    mul res(a.n);
    for(int i=0;i<m+1;i++)res.g[i][i]=1;
    while(b){
        if(b&1)res=res*a;
        a=a*a,b>>=1;
    }
    return res;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]),m+=!a[i];
	for(int i=0;i<=m;i++)A[i]=(m-i)*(m-i)%mod,B[i]=i*(n-2*m+i)%mod,C[i]=((n-1)*n/2-A[i]+mod-B[i]+mod)%mod;
	mul ans(m+1);
	ans.g[0][0]=C[0],ans.g[1][0]=A[0];
	ans.g[m][m]=C[m],ans.g[m-1][m]=B[m];
	for(int i=1;i<m;i++)ans.g[i][i]=C[i],ans.g[i-1][i]=B[i],ans.g[i+1][i]=A[i];
	ans=ksm(ans,k);
	ll cnt=0;
	for(int i=1;i<=m;i++)x+=!a[i];
	for(int i=0;i<=m;i++)cnt=(cnt+ans.g[i][x])%mod;
	printf("%lld",ans.g[m][x]*qpow(cnt,mod-2)%mod);
	return 0;
}
 		  			   		 			 	  						  	


Comments

Submit
0 Comments
More Questions

1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam